﻿using DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Models;
using Tools;
using System.Web.Security;


namespace WebUI.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/

        private DataContext db = new DataContext();
        private UserLogin UL = new UserLogin();
        private Encrypt encrypt = new Encrypt();
        public ActionResult LogIn()
        {
         
            return View();
        }

     
        /// <summary>
        /// 登录提交
        /// </summary>
        /// <param name="model"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LogIn(LoginModel model)
        {

            if (ModelState.IsValid)
            {
                string loginstate = "";
                loginstate = UL.Login(model.UserName, model.Password);

                if (loginstate == "0")
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    if (loginstate == "-1")
                    {
                        ViewBag.error = "用户名不存在!";
                    }
                    else if (loginstate == "-2")
                    {
                        ViewBag.error = "密码错误!";
                    }
                    else if (loginstate == "-3")
                    {
                        ViewBag.error = "帐号被禁用!";
                    }

                    return View(model);
                }
            }
            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            return View(model);
        }
        /// <summary>
        /// 注销
        /// </summary>
        public void LogOut()
        {
            FormsAuthentication.SignOut();
        }

        /// <summary>
        /// 密码修改页面
        /// </summary>
        /// <returns></returns>
        public ActionResult PassWordModify()
        {
            PassWordModifyModel model = new PassWordModifyModel();
            model.UserName = UserLogin.CreateUserInfo().UserName;

            return PartialView(model);
        }

        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PassWordModifySave(PassWordModifyModel model)
        {
            if (ModelState.IsValid)
            {
                string modifystate = "";
                modifystate = Modify(model.UserName, model.OldPassWord, model.NewPassWord);

                if (modifystate == "0")
                {

                    return Json(new { success = true });
                }
                else if (modifystate == "7")
                {

                    return Json(new { msg = "帐号不存在！" });
                }
                else
                {

                    return Json(new { msg = "原密码不正确！" });
                }
            }
            return Json(new { msg = "服务出现错误." });
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="username"></param>
        /// <param name="newpassword"></param>
        /// <returns></returns>
        public  string Modify(string username, string oldpassword, string newpassword)
        {


            DataContext db = new DataContext();

            var User = db.SysUsers.Where(n => n.UserName.Equals(username)).FirstOrDefault();
            if (User == null)
            {
                return "7";
            }
            oldpassword = encrypt.Encode(oldpassword);
            if (User.Password == oldpassword)
            {
                User.Password = encrypt.Encode(newpassword);

                db.SaveChanges();
                return "0";
            }
            else
            {
                return "-1";
            }

        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}
